package LEETCODE2023.BISHUA100;

import java.util.*;

public class SolutionBM53 {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 0, 2};
        System.out.println(minNumberDisappeared(nums));
        System.out.println(Arrays.toString(nums));

    }


    /**
     * 厉害的下标转换做法
     * @param nums
     * @return
     */
    public int minNumberDisappeared11 (int[] nums) {
        // write code here
        int[] arr = new int[nums.length + 2];
        for(int item : nums) {
            if(item >= 0 && item <= nums.length) {
                arr[item] = 1;
            }
        }
        //这种将数据转换成角标的过程，我们叫做Hash算法
        for(int i = 1; i < arr.length; i++) {
            if(arr[i] == 0) {
                return i;
            }
        }
        return 0;
    }

    /**
     * HashSet做法
     * @param nums
     * @return
     */
    public static int minNumberDisappeared1(int[] nums) {
        HashSet<Integer> hashSet = new HashSet<>();
        for (int i:nums
             ) {
            hashSet.add(i);
        }
        int res = 1;
        while(hashSet.contains(res)){
            res++;
        }
        return res;
    }

    /**
     * 过了但是很垃圾
     *
     */
    public static int minNumberDisappeared(int[] nums) {
        if(nums.length==0){
            return -1;
        }
        Arrays.sort(nums);
        int index = 0;
        while (index < nums.length && nums[index] <= 0) {
            index++;
        }
//        System.out.println(index);
        for (int i = index; i < nums.length; i++) {
            System.out.println("current" + i);
            if (nums[i] != i - index + 1) {
                return i - index + 1;
            }
        }
        return nums.length - index + 1;
//        if (nums.length < 1) {
//            return -1;
//        }
//        TreeSet<Integer> set = new TreeSet(Comparator.reverseOrder());
//        for (Integer i : nums
//        ) {
//            set.add(i);
//        }
//        int ans = set.iterator().next();
//        return ans >= 0 ? ans + 1 : 0;
    }
}
